package com.zqq.glide;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.NonNull;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Objects;

/**
 * @author zqq on 2018/7/11
 */
public class RoundCorner extends BitmapTransformation {

    private String ID = "com.zqq.glide.RoundCorner$leftTop$rightTop$leftBottom$rightBottom";

    private Context context;
    private Float leftTop = 0f;
    private Float rightTop = 0f;
    private Float leftBottom = 0f;
    private Float rightBottom = 0f;

//    private byte[] ID_BYTES;

    public RoundCorner(Context context, Float leftTop, Float rightTop, Float leftBottom, Float rightBottom) {
        this.context = context;
        this.leftTop = (float) dip2px(context, leftTop);
        this.rightTop = (float)dip2px(context, rightTop);
        this.leftBottom = (float)dip2px(context, leftBottom);
        this.rightBottom = (float) dip2px(context, rightBottom);
//        ID_BYTES = ID.getBytes();
    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        int width = toTransform.getWidth();
        int height = toTransform.getHeight();
        Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setHasAlpha(true);

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);

        BitmapShader shader = new BitmapShader(toTransform, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint.setShader(shader);

        RectF rectF = new RectF(0f, 0f, (float) width, height);
        float[] radii = {leftTop, leftTop, rightTop, rightTop, rightBottom, rightBottom, leftBottom, leftBottom};
        Path path = new Path();

        //添加圆角矩形曲线
        path.addRoundRect(rectF, radii, Path.Direction.CW);//顺时针

        canvas.drawPath(path, paint);
        return bitmap;
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
//        messageDigest.update(ID_BYTES);
    }

//    @Override
//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//        RoundCorner that = (RoundCorner) o;
//        return Objects.equals(context, that.context) &&
//                Objects.equals(leftTop, that.leftTop) &&
//                Objects.equals(rightTop, that.rightTop) &&
//                Objects.equals(leftBottom, that.leftBottom) &&
//                Objects.equals(rightBottom, that.rightBottom);
//    }
//
//    @Override
//    public int hashCode() {
//        return Objects.hash(ID, leftTop, rightTop, leftBottom, rightBottom);
//    }

    //dp转px
    private int dip2px(Context context, Float value) {
        float var2 = context.getResources().getDisplayMetrics().density;
        return (int) (value * var2 + 0.5f);
    }


}
